<?php
namespace App\Repositories\Admin;

use App\Jobs\Fun;
use App\Models\Admin\CompanyModel;
use App\Models\Admin\JobCompanyModel;
use App\Models\Admin\JobModel;
use App\Models\Admin\UserModel;
use App\Models\JobsCompanysModel;
use App\Models\UserStaffModel;
use App\Repositories\BaseRepository;

class JobCompanyRepository extends BaseRepository
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new JobCompanyModel();
    }

//    /**
//     * 操作分页获取
//     */
//    public function getIndexList()
//    {
//        $models = $this->repository
//            ->orderBy('id','desc')
//            ->paginate(10);
//        $models->limit = 10;
//        return $models;
//    }

    /**
     * 后台获取
     */
    public function getAdminIndex()
    {
        $models = $this->repository
            ->orderBy('id','desc')
            ->paginate(10);
        $models->limit = 10;
        if (!count($models)) { return ''; }
        foreach ($models as $model) {
            $company = CompanyModel::where('uid',$model->uid)->first();
            $jobModel = JobModel::where('id',$model->job)->first();
            $ages = explode(',',$model->age_range);
            $model->cname = (isset($company)&&$company->com_name)
                ? $company->com_name : '未知';
            $model->jobName = $jobModel->name;
            $model->ageStart = $ages[0];;
            $model->agEnd = $ages[1];;
        }
        return $models;
    }

    /**
     * 通过企业UID，获取
     */
    public function getIndexByCid($uid)
    {
        return $this->repository->where('cid',$uid)
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * APP所有岗位列表
     */
    public function getAppJobList($page=0)
    {
       return JobsCompanysModel::getAppJobList($page);
    }

    /**
     * 通过CID，获取某个企业岗位
     */
    public function getAppJobsByCid($cid,$page=0)
    {
        $models = $this->repository
            ->where('cid',$cid)
            ->where('del',0)
            ->orderBy('id','desc')
            ->skip($page)
            ->take(20)
            ->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $key => $model) {
            $jobModel = JobModel::where('id',$model->job)->first();
            $userinfo=\App\Models\User\UserModel::getinfo($model->cid);
            $dataArr[$key]['id'] = $model->id;
            $dataArr[$key]['jobName'] = $jobModel ? $jobModel->name : '未知岗位';



            $dataArr[$key]['head'] = \App\Jobs\Fun::pImg($userinfo['head']);
            $dataArr[$key]['cname'] = $userinfo['name'];
            $dataArr[$key]['contact'] = $model->contact_name;
            $dataArr[$key]['address'] = $model->address;
        }
        return $dataArr;
    }

    /**
     * APP岗位推荐
     */
    public function getAppJobComs()
    {
        return $this->repository
            ->where('status',JobsCompanysModel::status_fabuzhong)
            ->where('del',0)
            ->orderBy('paixu','desc')
            ->orderBy('id','desc')
            ->skip(0)
            ->take(3)
            ->get();
    }

    public function setAdd($data)
    {
        $data['created_at'] = time();
       // print_r($data);exit;
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$id)
    {
        $data['updated_at'] = time();
        return $this->repository->where('id',$id)
            ->update($data);
    }

    public function getShowOne($id)
    {
        return $this->repository
            ->where('id',$id)
            ->first();
    }

    public function getAdminShow($id)
    {
        $model = $this->repository
            ->where('id',$id)
            ->first();
        if ($model && $model->age_range) {
            $ages = explode(',',$model->age_range);
            $model->ageStart = $ages[0];
            $model->ageEnd = $ages[1];
        } else {
            $model->ageStart = 0;
            $model->ageEnd = 0;
        }
        return $model;
    }

    /**
     * APP端详情
     */
    public function getAppShowOne($id)
    {
        $model = $this->repository
            ->where('id',$id)
            ->where('del',0)
            ->first();
        if(!$model){
            return null;
        }
       // $user = UserModel::where('id',$model->cid)->first();
        //$com = CompanyModel::where('uid',$model->cid)->first();
        $jobModel = JobModel::where('id',$model->job)->first();
       /* if ($user&&$user->head&&file_exists(ltrim($user->head,'/'))) {
            $model->head = $user->head;
        } else {
            $model->head = '';
        }*/
        $model->contact = $model->contact_name ? $model->contact_name : '未定义';
        $model->jobName = $jobModel ? $jobModel->name : '未定义';
        $model->sexName =   \App\Models\User\UserModel::sexTo($model->sex);
        $model->telphone = $model->contact_telephone ?
            $model->contact_telephone : '未定义';
       /// var_dump($model->cid,$com);exit;
        // $model->address = $com->address ? $com->address : '未定义';
      //  if($com){
       //     $model->cname = $com->com_name ? $com->com_name : '未定义';
       //     $model->email = $com->email ? $com->email : '未定义';
    //    }


        $model->restName = $model->rest==1 ? '单休' : '双休';
        $model->workYear = $model->getYears($model->work_year);
        $model->workHour = $model->work_hour ? $model->work_hour : '未定义';
        $model->ageRange = $model->age_range;
        $edus = $this->getEdus();
        $model->eduName = array_key_exists($model->education,$edus) ?
            $edus[$model->education] : '未定义';
        return $model;
    }

    /**
     * 获取所有企业
     */
    public function getCompanys()
    {
        $models = CompanyModel::orderBy('id','asc')->get();
        if (!count($models)) { return ''; }
        $dataArr = array();
        $dataArr[0]['uid'] = 0;
        $dataArr[0]['name'] = '选择';
        foreach ($models as $key => $model) {
            $user = UserModel::where('id',$model->uid)->first();
            if (!$user->is_delete) {
                $dataArr[$key+1]['uid'] = $model->uid;
                $dataArr[$key+1]['name'] = $model->com_name;
                $dataArr[$key+1]['uid'] = $model->uid;
                $dataArr[$key+1]['name'] = $model->com_name;
            }
        }
        return $dataArr;
    }

    /**
     * 获取福利待遇
     */
    public static function getWelfares($type=0)
    {
        $list=array(
            '暂无', '包住', '包吃','包吃包住',
        );
        if($type===0){
            return  implode(',',$list);
        }
        return isset($list[$type])?$list[$type]:'未知';
    }

    /**
     * 获取休息制度
     */
    public function getRests()
    {
        return array(
            1 => '单休', '双休',
        );
    }

    /**
     * 获取学历
     */
    public function getEdus()
    {
        return array(
            1 => '小学', '初中', '高中', '大专', '本科', '研究生', '其他',
        );
    }

    /**
     * 获取工作年限
     */
    public function getWorkYears()
    {
        return array(
            1 => '一年以内', '一至二年', '三至五年', '五年以上',
        );
    }


    /**
     * 获取所有岗位
     * @param $cate 1工厂，2公司
     */
    public function getJobs($cate)
    {
        if (!in_array($cate,[1,2])) { return false; }
        $models = JobModel::where('cate',$cate)
            ->where('pid','>',0)
            ->orderBy('id','desc')
            ->get();
        if (!count($models)) { return ''; }
        $dataArr = array();
        foreach ($models as $key => $model) {
            $job = JobModel::where('id',$model->pid)->first();
            $dataArr[$key]['id'] = $model->id;
//            $dataArr[$key]['name'] = $model->name;
//            $dataArr[$key]['pid'] = $model->pid;
//            $dataArr[$key]['pname'] = $job?$job->name:''; //部门名称
            $pname = $job?$job->name:'无'; //部门名称
            $dataArr[$key]['name'] = $pname.'--'.$model->name;
        }
        return $dataArr;
    }

    /**
     * 获取企业
     */
    public function getComByUid($uid)
    {
        return CompanyModel::where('uid',$uid)->first();
    }

    /**
     * 设置用户删除
     */
    public function setDel($id)
    {
        return $this->repository
            ->where('id',$id)
            ->where('del',0)
            ->update(['del'=>1]);
    }
}